package com.iad.controller.command
{
  import com.adobe.cairngorm.commands.Command;
  import com.adobe.cairngorm.control.CairngormEvent;
  import com.adobe.cairngorm.control.CairngormEventDispatcher;
  import com.iad.controller.event.*;
  import com.iad.controller.manager.*;
  import com.iad.controller.service.*;
  import com.iad.model.*;
  import com.iad.model.valueobject.*;
  import com.iad.view.component.*;
  import com.iad.view.form.*;
  import com.iad.view.util.ascb.util.*;
  
  import flash.events.*;
  import flash.net.*;
  
  import mx.controls.Image;
  import mx.core.IFlexDisplayObject;
  import mx.core.UIComponent;
  import mx.events.DragEvent;
  import mx.formatters.NumberFormatter;
  import mx.managers.PopUpManager;
    
  
  public class UploadCommand implements Command {
    [Bindable] private var model:ModelLocatorImpl = ModelLocatorImpl.getInstance();
    
    private var numberFormatter:NumberFormatter = new NumberFormatter();
    private var currentEditSiteId:int;
    private var page:Page;
    private var discography:Discography;
    private var mp3Player:MP3_Player;
    private var siteButtonEdit:SiteButtonEdit;
    
    private var signupSiteImageFileRef:FileReference;
    private var signupPageImageFileRef:FileReference;
    private var newSiteImageFileRef:FileReference;
    private var siteImageFileRef:FileReference;
    private var siteEditImageFileRef:FileReference;
    private var pageImageFileRef:FileReference;
    
    private var playlistUpdateImageFileRef:FileReference;
    private var playlistImageFileRef:FileReference;
    private var signupPlaylistImageFileRef:FileReference;
    private var songFileRef:FileReference;
    private var videoImageFileRef:FileReference;
    private var videoFileRef:FileReference;
    
    private var signupImageForm:SignupImageUpload; 
    private var newVideoForm:NewVideoForm; 
    private var newPlaylistForm:NewPlaylistForm; 
    private var playlistName:String; 
    private var songUploadForm:SongUploadForm; 
    private var siteEditImage:Image;
    
    private var audioFilter:FileFilter = new FileFilter("Audio Files (*.mp3)", "*.mp3;");
    private var imagesFilter:FileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");
    private var videoFilter:FileFilter = new FileFilter("Video", "*.flv");
    
    
    public function execute(event:CairngormEvent):void {
      switch (event.type) {
        case UploadEvent.UPLOAD_SIGNUP_SITE_IMAGE:
          uploadSignupSiteImage();
        break;
        case UploadEvent.UPLOAD_NEW_SITE_IMAGE:
          uploadNewSiteImage(event as UploadEvent);
        break;
        case UploadEvent.UPLOAD_SITE_IMAGE:
          uploadSiteImage(event as UploadEvent);
        break;
        case UploadEvent.UPLOAD_SITE_EDIT_IMAGE:
          uploadSiteEditImage(event as UploadEvent);
        break;
        case UploadEvent.UPLOAD_PAGE_IMAGE:
          uploadPageImage(event as UploadEvent);
        break;
        case UploadEvent.UPLOAD_SIGNUP_PAGE_IMAGE:
          uploadSignupPageImage(event as UploadEvent);
        break;
         case UploadEvent.UPLOAD_SIGNUP_PLAYLIST_IMAGE:
          uploadSignupPlaylistImage(event as UploadEvent);
        break;
        case UploadEvent.UPLOAD_NEW_PLAYLIST_IMAGE:
          uploadNewPlaylistImage(event as UploadEvent);
        break;
        case UploadEvent.UPLOAD_PLAYLIST_IMAGE:
          uploadPlaylistImage(event as UploadEvent);
        break;
        case UploadEvent.UPLOAD_VIDEO_IMAGE:
          uploadVideoImage(event as UploadEvent);
        break;
        case UploadEvent.UPLOAD_VIDEO:
          uploadVideo(event as UploadEvent);
        break;
        case UploadEvent.UPLOAD_SONG:
          uploadSong(event as UploadEvent);
        break;
        case UploadEvent.UPLOAD_SIGNUP_SONG:
          uploadSignupSong(event as UploadEvent);
        break;
     }    
   }
   
   
   private function playlistUpdateImageFileRefSelect(event:Event):void {
     if (playlistUpdateImageFileRef.size > 512000){
       model.progressMessage = "ERROR: Max File Size is: 512 Kb";
       return;
     }
     try {
       model.progressMessage = "size (bytes): " + numberFormatter.format(playlistUpdateImageFileRef.size);
       var params:URLVariables = new URLVariables();
       params.path = "site/sites/" + model.currentSiteId + "/images/" + playlistUpdateImageFileRef.name;
       params.playlistId = model.currentPlaylistId.toString();
       params.isPlaylistUpdate = true;
       var request:URLRequest = new URLRequest("http://"+model.SERVER+":"+model.PORT+"/"+ model.CONTEXT+"/UploadExample");
       request.method = URLRequestMethod.POST;
       request.data = params;
       model.progressBarVisible = true;
       playlistUpdateImageFileRef.upload(request); 
     } 
     catch (err:Error) {
       model.progressMessage = "ERROR: zero-byte file";
     }
   }
   
   
       
  private function videoImageFileRefSelect(event:Event):void {
   if (videoImageFileRef.size > 512000){
     model.progressMessage = "ERROR: Max File Size is: 512 Kb";
     return;
   }
   try {
     model.progressMessage = "size (bytes): " + numberFormatter.format(videoImageFileRef.size);
     var params:URLVariables = new URLVariables();
     params.path = "site/sites/" + model.currentSiteId + "/images/" + videoImageFileRef.name;
     params.id = model.currentVideoId.toString();
     params.siteId = model.currentSiteId.toString();
     params.isVideoUpdate = true;
     var request:URLRequest = new URLRequest("http://"+model.SERVER+":"+model.PORT+"/"+ model.CONTEXT+"/UploadExample");
     request.method = URLRequestMethod.POST;
     request.data = params;
     model.progressBarVisible = true;
     videoImageFileRef.upload(request); 
   } 
   catch (err:Error) {
     model.progressMessage = "ERROR: zero-byte file";
   }

  }
     
     
    private function uploadPlaylistImage(event:UploadEvent):void {
      discography = event.object as Discography;
      playlistUpdateImageFileRef = new FileReference();
      playlistUpdateImageFileRef.addEventListener(Event.SELECT, playlistUpdateImageFileRefSelect);
      playlistUpdateImageFileRef.addEventListener(ProgressEvent.PROGRESS, fileRefProgress);
      playlistUpdateImageFileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, fileRefComplete);
      playlistUpdateImageFileRef.browse([imagesFilter]);
      model.progressMessage = "";
    }
    
    private function uploadVideoImage(event:UploadEvent):void {
      videoImageFileRef = new FileReference();
      videoImageFileRef.addEventListener(Event.SELECT, videoImageFileRefSelect);
      videoImageFileRef.addEventListener(ProgressEvent.PROGRESS, fileRefProgress);
      videoImageFileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, fileRefComplete);
      videoImageFileRef.browse([imagesFilter]);
      model.progressMessage = "";
    }
     
    private function uploadNewPlaylistImage(event:UploadEvent):void {
      discography = event.object as Discography;
      playlistImageFileRef = new FileReference();
      playlistImageFileRef.addEventListener(Event.SELECT, playlistImageFileRefSelect);
      playlistImageFileRef.addEventListener(ProgressEvent.PROGRESS, fileRefProgress);
      playlistImageFileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, fileRefComplete);
      playlistImageFileRef.browse([imagesFilter]);
      model.progressMessage = "";
    }
    
    private function uploadSignupPlaylistImage(event:UploadEvent):void {
      playlistName = event.object as String;
      signupPlaylistImageFileRef = new FileReference();
      signupPlaylistImageFileRef.addEventListener(Event.SELECT, signupPlaylistImageFileRefSelect);
      signupPlaylistImageFileRef.addEventListener(ProgressEvent.PROGRESS, signupPlaylistImageFileRefProgress);
      signupPlaylistImageFileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, signupPlaylistImageFileRefComplete);
      signupPlaylistImageFileRef.browse([imagesFilter]);
      model.progressMessage = "";
      model.signupWizard.playlistName.editable = false;
      model.signupWizard.updatePlaylistButton.includeInLayout = false;
      model.signupWizard.updatePlaylistButton.visible = false;
      model.signupWizard.playlistCreatedLabel.includeInLayout = true;
      model.signupWizard.playlistCreatedLabel.visible = true;
    }
    
    
    private function uploadVideo(event:UploadEvent):void {
      discography = event.object as Discography;
      videoFileRef = new FileReference();
      videoFileRef.addEventListener(Event.SELECT, videoFileRefSelect);
      videoFileRef.addEventListener(ProgressEvent.PROGRESS, fileRefProgress);
      videoFileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, fileRefComplete);
      videoFileRef.browse([videoFilter]);
      model.progressMessage = "";
    }
    
    
    private function uploadSignupSong(event:UploadEvent):void {
      songFileRef = new FileReference();
      songFileRef.addEventListener(Event.SELECT, signupSongFileRefSelect);
      songFileRef.addEventListener(ProgressEvent.PROGRESS, songFileRefProgress);
      songFileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, signupSongFileRefComplete);
      songFileRef.browse([audioFilter]);
      model.progressMessage = "";
    }
    
    
    private function signupSongFileRefSelect(event:Event):void {
      
     if (songFileRef.size > 10000000){
       model.progressMessage = "ERROR: Max File Size is: 10 Mb";
       return;
     }
   
     try {
       model.progressMessage = "size (bytes): " + numberFormatter.format(songFileRef.size);
       var params:URLVariables = new URLVariables();
       params.path = "site/sites/" + model.currentSiteId + "/audio/" + songFileRef.name;
       params.songName = model.signupWizard.songName.text;
       params.albumName = model.signupWizard.albumName.text;
       params.isNewSong = true;
       params.siteId = model.playlistSiteId.toString();
       params.playlistId = model.currentPlaylistId.toString();
       var request:URLRequest = new URLRequest("http://"+model.SERVER+":"+model.PORT+"/"+ model.CONTEXT+"/UploadExample");
       request.method = URLRequestMethod.POST;
       request.data = params;
       model.progressBarVisible = true;
       songFileRef.upload(request); 
     } 
     catch (err:Error) {
       model.progressMessage = "ERROR: zero-byte file";
     }
  }
    
    
    private function uploadSong(event:UploadEvent):void {
      mp3Player = event.object as MP3_Player;
      songFileRef = new FileReference();
      songFileRef.addEventListener(Event.SELECT, songFileRefSelect);
      songFileRef.addEventListener(ProgressEvent.PROGRESS, songFileRefProgress);
      songFileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, songFileRefComplete);
      songFileRef.browse([audioFilter]);
      model.progressMessage = "";
    }
    
    
     private function songFileRefSelect(event:Event):void {
       songUploadForm = SongUploadForm(PopUpManager.createPopUp(mp3Player, SongUploadForm , true));
       PopUpManager.centerPopUp(songUploadForm);
       songUploadForm.fileName = songFileRef.name;
       songUploadForm.addEventListener("close", songUploadFormCancel);
       songUploadForm.addEventListener("songUploadFormComplete", songUploadFormClose);
     }
     
     
    private function songUploadFormCancel(event:Event):void {
      PopUpManager.removePopUp(event.target as IFlexDisplayObject);
    }
    
    
    private function songUploadFormClose(event:Event):void {
      var form:SongUploadForm = event.target as SongUploadForm;
      PopUpManager.removePopUp(event.target as IFlexDisplayObject);
      event.target.removeEventListener("songUploadFormComplete", songUploadFormClose);
      
     if (songFileRef.size > 10000000){
       model.progressMessage = "ERROR: Max File Size is: 10 Mb";
       return;
     }
   
     try {
       model.progressMessage = "size (bytes): " + numberFormatter.format(songFileRef.size);
       var params:URLVariables = new URLVariables();
       params.path = "site/sites/" + model.playlistSiteId + "/audio/" + songFileRef.name;
       params.songName = form.songName.text;
       params.albumName = form.albumName.text;
       params.isNewSong = true;
       params.siteId = model.playlistSiteId.toString();
       params.playlistId = model.currentSite.currentPlaylistId.toString();
       var request:URLRequest = new URLRequest("http://"+model.SERVER+":"+model.PORT+"/"+ model.CONTEXT+"/UploadExample");
       request.method = URLRequestMethod.POST;
       request.data = params;
       model.progressBarVisible = true;
       songFileRef.upload(request); 
     } 
     catch (err:Error) {
       model.progressMessage = "ERROR: zero-byte file";
     }
  }
  
    private function songFileRefProgress(event:ProgressEvent):void {
    model.progressBarVisible = true;
  }


  private function songFileRefComplete(event:Event):void {
    model.progressMessage += " (complete)";
    model.progressBarVisible = false;
    CairngormEventDispatcher.getInstance().dispatchEvent(new PlaylistEvent(PlaylistEvent.GET_PLAYLIST, model.currentSite.currentPlaylistId));
  }
  
  
  private function signupSongFileRefComplete(event:Event):void {
    model.progressMessage += " (complete)";
    model.progressBarVisible = false;
    CairngormEventDispatcher.getInstance().dispatchEvent(new PlaylistEvent(PlaylistEvent.GET_PLAYLIST, model.currentPlaylistId));
  }
    
    
   private function videoFileRefSelect(event:Event):void {
     newVideoForm = NewVideoForm(PopUpManager.createPopUp(discography, NewVideoForm , true));
     PopUpManager.centerPopUp(newVideoForm);
     newVideoForm.fileName = videoFileRef.name;
     newVideoForm.addEventListener("close", formCancel);
     newVideoForm.addEventListener("newVideoFormComplete", newVideoFormClose);
   }
   
   
  private function newVideoFormClose(event:Event):void {
    var form:NewVideoForm = event.target as NewVideoForm;
    PopUpManager.removePopUp(event.target as IFlexDisplayObject);
    event.target.removeEventListener("newVideoFormComplete", newVideoFormClose);

    if (videoFileRef.size > 30720000){
      model.progressMessage = "ERROR: Max File Size is: 30 Mb";
      return;
    }
   
   try {
     model.progressMessage = "size (bytes): " + numberFormatter.format(videoFileRef.size);
     var params:URLVariables = new URLVariables();
     params.path = "site/sites/" + model.currentSiteId + "/video/" + videoFileRef.name;
     params.isNewVideo = true;
     var request:URLRequest = new URLRequest("http://"+model.SERVER+":"+model.PORT+"/"+ model.CONTEXT+"/UploadExample");
     params.videoName = form.videoName.text;
     params.siteId = model.currentSiteId.toString();
     request.method = URLRequestMethod.POST;
     request.data = params;
     model.progressBarVisible = true;
     videoFileRef.upload(request); 
   } 
   catch (err:Error) {
     model.progressMessage = "ERROR: zero-byte file";
   }
  }
    
    
    private function playlistImageFileRefSelect(event:Event):void {
      newPlaylistForm = NewPlaylistForm(PopUpManager.createPopUp(discography, NewPlaylistForm , true));
      PopUpManager.centerPopUp(newPlaylistForm);
      newPlaylistForm.fileName = playlistImageFileRef.name;
      newPlaylistForm.addEventListener("close", formCancel);
      newPlaylistForm.addEventListener("newPlaylistFormComplete", newPlaylistFormClose);
    }
    
    
    private function formCancel(event:Event):void {
      PopUpManager.removePopUp(event.target as IFlexDisplayObject);
    }
    
    
  private function newPlaylistFormClose(event:Event):void {
    var form:NewPlaylistForm = event.target as NewPlaylistForm;
    PopUpManager.removePopUp(event.target as IFlexDisplayObject);
    event.target.removeEventListener("newPlaylistFormComplete", newPlaylistFormClose);
     
   if (playlistImageFileRef.size > 512000){
     model.progressMessage = "ERROR: Max File Size is: 512 Kb";
     return;
   }
   
   try {
     model.progressMessage = "size (bytes): " + numberFormatter.format(playlistImageFileRef.size);
     var params:URLVariables = new URLVariables();
     params.path = "site/sites/" + model.currentSiteId + "/images/" + playlistImageFileRef.name;
     params.playlistName = form.playlistName.text;
     params.siteId = model.currentSiteId.toString();
     params.isNewPlaylist = true;
     var request:URLRequest = new URLRequest("http://"+model.SERVER+":"+model.PORT+"/"+ model.CONTEXT+"/UploadExample");
     request.method = URLRequestMethod.POST;
     request.data = params;
     model.progressBarVisible = true;
     playlistImageFileRef.upload(request); 
   } 
   catch (err:Error) {
     model.progressMessage = "ERROR: zero-byte file";
   }
  }
  
  
    private function uploadSignupSiteImage():void {
      signupSiteImageFileRef = new FileReference();
      signupSiteImageFileRef.addEventListener(Event.SELECT, signupSiteImageFileRefSelect);
      signupSiteImageFileRef.addEventListener(ProgressEvent.PROGRESS, signupSiteImageFileRefProgress);
      signupSiteImageFileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, signupSiteImageFileRefComplete);
      var imagesFilter:FileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");
      signupSiteImageFileRef.browse([imagesFilter]);
      model.progressMessage = "";
    }

    
    private function uploadSiteImage(event:UploadEvent):void {
      currentEditSiteId = ((event.object as DragEvent).currentTarget as SiteButton).site.id;
      siteImageFileRef = new FileReference();
      siteImageFileRef.addEventListener(Event.SELECT, siteImageFileRefSelect);
      siteImageFileRef.addEventListener(ProgressEvent.PROGRESS, siteImageFileRefProgress);
      siteImageFileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, siteImageFileRefComplete);
      var imagesFilter:FileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");
      siteImageFileRef.browse([imagesFilter]);
      model.progressMessage = "";
    }
    
    
    private function uploadSiteEditImage(event:UploadEvent):void {
      siteEditImage = event.object as Image;
      siteEditImageFileRef = new FileReference();
      siteEditImageFileRef.addEventListener(Event.SELECT, siteEditImageFileRefSelect);
      siteEditImageFileRef.addEventListener(ProgressEvent.PROGRESS, siteEditImageFileRefProgress);
      siteEditImageFileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, siteEditImageFileRefComplete);
      var imagesFilter:FileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");
      siteEditImageFileRef.browse([imagesFilter]);
      model.progressMessage = "";
    }
    
    
    private function uploadNewSiteImage(event:UploadEvent):void {
      var dragEvent:DragEvent = event.object as DragEvent;
      if(UIComponent(dragEvent.dragInitiator).name == model.NEW_SITE) {
        var site:Site = new Site();
        site.name = "New Site";
        site.active = true;
        model.currentSite = site;
          
        siteButtonEdit = new SiteButtonEdit();
        siteButtonEdit.x = 755;
        siteButtonEdit.y = 110;
        siteButtonEdit.width = 140;
        siteButtonEdit.height = 130;
        siteButtonEdit.site = site;
        model.app.mainView.addChild(siteButtonEdit);
        model.NEW_SITE_MODE = true;
        newSiteImageFileRef = new FileReference();
        newSiteImageFileRef.addEventListener(Event.SELECT, newSiteImageFileRefSelect);
        newSiteImageFileRef.addEventListener(ProgressEvent.PROGRESS, newSiteImageFileRefProgress);
        newSiteImageFileRef.addEventListener(Event.COMPLETE, newSiteImageFileRefComplete);
        newSiteImageFileRef.browse([imagesFilter]);
        model.progressMessage = "";
      }
    }
    
    
    private function uploadPageImage(event:UploadEvent):void {
      page = event.object as Page;
      pageImageFileRef = new FileReference();
      pageImageFileRef.addEventListener(Event.SELECT, pageImageFileRefSelect);
      pageImageFileRef.addEventListener(ProgressEvent.PROGRESS, pageImageFileRefProgress);
      pageImageFileRef.addEventListener(Event.COMPLETE, pageImageFileRefComplete);
      pageImageFileRef.browse([imagesFilter]);
      model.progressMessage = "";
    }
    
    
    private function uploadSignupPageImage(event:UploadEvent):void {
      signupImageForm = event.object as SignupImageUpload;
      signupPageImageFileRef = new FileReference();
      signupPageImageFileRef.addEventListener(Event.SELECT, signupPageImageFileRefSelect);
      signupPageImageFileRef.addEventListener(ProgressEvent.PROGRESS, signupPageImageFileRefProgress);
      signupPageImageFileRef.addEventListener(Event.COMPLETE, signupPageImageFileRefComplete);
      signupPageImageFileRef.browse([imagesFilter]);
      model.progressMessage = "";
    }
    
    
    private function newSiteImageFileRefSelect(event:Event):void {
      if (event.currentTarget.size > 256000){
        model.progressMessage = "ERROR: Max File Size is: 256 Kb";
        return;
      }
      try {
        model.progressMessage = "size (bytes): " + numberFormatter.format(newSiteImageFileRef.size);
        var params:URLVariables = new URLVariables();
        params.path = "site/sites/0/images/" + newSiteImageFileRef.name;
        var request:URLRequest = new URLRequest("http://"+model.SERVER+":"+model.PORT+"/"+ model.CONTEXT+"/UploadExample");
        request.method = URLRequestMethod.POST;
        request.data = params;
        model.progressBarVisible = true;
        newSiteImageFileRef.upload(request); 
      } 
      catch (err:Error) {
        model.progressMessage = "ERROR: zero-byte file";
      }
    }
    
    
    private function siteImageFileRefSelect(event:Event):void {
      if (siteImageFileRef.size > 512000){
        model.progressMessage = "ERROR: Max File Size is: 512 Kb";
        return;
      }
      try {
        model.progressMessage = "size (bytes): " + numberFormatter.format(siteImageFileRef.size);
        var params:URLVariables = new URLVariables();
        params.path = "site/sites/" + currentEditSiteId + "/images/" + siteImageFileRef.name;
        params.siteId = currentEditSiteId.toString();
        params.isSiteUpdate = true;
        var request:URLRequest = new URLRequest("http://"+model.SERVER+":"+model.PORT+"/"+ model.CONTEXT+"/UploadExample");
        request.method = URLRequestMethod.POST;
        request.data = params;
        model.progressBarVisible = true;
        siteImageFileRef.upload(request); 
      } 
      catch (err:Error) {
        model.progressMessage = "ERROR: zero-byte file";
      }
    }
    
    
    private function siteEditImageFileRefSelect(event:Event):void {
      if (siteEditImageFileRef.size > 512000){
        model.progressMessage = "ERROR: Max File Size is: 512 Kb";
        return;
      }
      try {
        model.progressMessage = "size (bytes): " + numberFormatter.format(siteEditImageFileRef.size);
        var params:URLVariables = new URLVariables();
        params.path = "site/sites/" + model.currentSiteId + "/images/" + siteEditImageFileRef.name;
        params.siteId = model.currentSiteId.toString();
        params.isSiteImageUpdate = true;
        var request:URLRequest = new URLRequest("http://"+model.SERVER+":"+model.PORT+"/"+ model.CONTEXT+"/UploadExample");
        request.method = URLRequestMethod.POST;
        request.data = params;
        model.progressBarVisible = true;
        siteEditImageFileRef.upload(request); 
      } 
      catch (err:Error) {
        model.progressMessage = "ERROR: zero-byte file";
      }
    }
    
    private function signupSiteImageFileRefSelect(event:Event):void {
      if (event.currentTarget.size > 128000){
        model.progressMessage = "ERROR: Max File Size is: 128 Kb";
        return;
      }
      try {
        model.progressMessage = "size (bytes): " + numberFormatter.format(signupSiteImageFileRef.size);
        var params:URLVariables = new URLVariables();
        params.path = "site/sites/0/images/" + signupSiteImageFileRef.name;
        var request:URLRequest = new URLRequest("http://"+model.SERVER+":"+model.PORT+"/"+ model.CONTEXT+"/UploadExample");
        request.method = URLRequestMethod.POST;
        request.data = params;
        model.progressBarVisible = true;
        signupSiteImageFileRef.upload(request); 
      } 
      catch (err:Error) {
        model.progressMessage = "ERROR: zero-byte file";
      }
    }
    
   
    private function pageImageFileRefSelect(event:Event):void {
      if (event.currentTarget.size > 512000){
        model.progressMessage = "ERROR: Max File Size is: 500 KB";
        return;
      }
      try {
        model.progressMessage = "size (bytes): " + numberFormatter.format(pageImageFileRef.size);
        var params:URLVariables = new URLVariables();
      
        if (model.app.currentState == model.ARTIST_VIEW) {
          params.path = "site/sites/" + model.currentSiteId + "/images/" + pageImageFileRef.name;
        }
        else {
          params.path = "site/" + page.name + "/" + pageImageFileRef.name;
        }
        
        var request:URLRequest = new URLRequest("http://"+model.SERVER+":"+model.PORT+"/"+ model.CONTEXT+"/UploadExample");
        request.method = URLRequestMethod.POST;
        request.data = params;
        model.progressBarVisible = true;
        pageImageFileRef.upload(request); 
      } 
      catch (err:Error) {
        model.progressMessage = "ERROR: zero-byte file";
      }
    }
    
    
    private function signupPageImageFileRefSelect(event:Event):void {
      if (event.currentTarget.size > 512000){
        signupImageForm.progressMessage.text = "ERROR: Max File Size is: 500 KB";
        return;
      }
      try {
        signupImageForm.progressMessage.text = "size (bytes): " + numberFormatter.format(signupPageImageFileRef.size);
        var params:URLVariables = new URLVariables();
        params.path = "site/sites/" + model.currentSiteId + "/images/" + signupPageImageFileRef.name;
        
        var request:URLRequest = new URLRequest("http://"+model.SERVER+":"+model.PORT+"/"+ model.CONTEXT+"/UploadExample");
        request.method = URLRequestMethod.POST;
        request.data = params;
        signupImageForm.progressBar.visible = true;
        signupPageImageFileRef.upload(request); 
      } 
      catch (err:Error) {
        signupImageForm.progressMessage.text = "ERROR: zero-byte file";
      }
    }
    
    
    private function signupPlaylistImageFileRefSelect(event:Event):void {
      if (event.currentTarget.size > 512000){
        model.progressMessage = "ERROR: Max File Size is: 500 KB";
        return;
      }
      try {
        model.progressMessage = "size (bytes): " + numberFormatter.format(signupPlaylistImageFileRef.size);
        var params:URLVariables = new URLVariables();
        params.path = "site/sites/" + model.currentSiteId + "/images/" + signupPlaylistImageFileRef.name;
        params.playlistName = playlistName;
        params.siteId = model.currentSiteId.toString();
        params.isPlaylistUpdateDetails = true;
        var request:URLRequest = new URLRequest("http://"+model.SERVER+":"+model.PORT+"/"+ model.CONTEXT+"/UploadExample");
        request.method = URLRequestMethod.POST;
        request.data = params;
        model.progressBarVisible = true;
        signupPlaylistImageFileRef.upload(request); 
      } 
      catch (err:Error) {
        signupImageForm.progressMessage.text = "ERROR: zero-byte file";
      }
    }
  
  
    private function signupPageImageFileRefProgress(event:ProgressEvent):void {
      signupImageForm.progressBar.visible = true;
    }
  
  
    private function signupSiteImageFileRefProgress(event:ProgressEvent):void {
      model.progressBarVisible = true;
    }
  
    private function newSiteImageFileRefProgress(event:ProgressEvent):void {
      model.progressBarVisible = true;
    }
  
    private function siteImageFileRefProgress(event:ProgressEvent):void {
      model.progressBarVisible = true;
    }
    
    
    private function siteEditImageFileRefProgress(event:ProgressEvent):void {
      model.progressBarVisible = true;
    }
    
    private function singupPlaylistImageFileRefProgress(event:ProgressEvent):void {
      model.progressBarVisible = true;
    }
  
    private function pageImageFileRefProgress(event:ProgressEvent):void {
      model.progressBarVisible = true;
    }


    private function newSiteImageFileRefComplete(event:Event):void {
      model.progressMessage += " (complete)";
      model.progressBarVisible = false;
      model.currentSite.imagePath = newSiteImageFileRef.name;
      siteButtonEdit.siteIcon.source = "site/sites/0/images/" + newSiteImageFileRef.name;
    }
    
    
    private function siteEditImageFileRefComplete(event:Event):void {
      model.progressMessage += " (complete)";
      model.progressBarVisible = false;
      model.currentSite.imagePath = siteEditImageFileRef.name;
      siteEditImage.source = "site/sites/" + model.currentSiteId + "/images/" + siteEditImageFileRef.name;
    }
    
    private function siteImageFileRefComplete(event:Event):void {
      model.progressMessage += " (complete)";
      model.progressBarVisible = false;
      CairngormEventDispatcher.getInstance().dispatchEvent(new SiteEvent(SiteEvent.GET_SITES, null));
    }
    
    private function signupSiteImageFileRefComplete(event:Event):void {
      model.progressMessage += " (complete)";
      model.progressBarVisible = false;
      model.signupSiteImagePath = "site/sites/0/images/" + signupSiteImageFileRef.name;
      model.signupSiteImageName = signupSiteImageFileRef.name;
    }
    
    private function signupPlaylistImageFileRefComplete(event:Event):void {
      model.progressMessage += " (complete)";
      model.progressBarVisible = false;
      model.signupPlaylistImagePath = "site/sites/" + model.currentSiteId + "/images/" + signupPlaylistImageFileRef.name;
      model.signupPlaylistImageName = signupPlaylistImageFileRef.name;
    }
    
    
    private function pageImageFileRefComplete(event:Event):void {
      model.progressMessage += " (complete)";
      model.progressBarVisible = false;
    
      if (model.app.currentState == model.ARTIST_VIEW) {
        model.latestUpload.source = "site/sites/" + model.currentSiteId + "/images/" + pageImageFileRef.name;
      }
      else {
        model.latestUpload.source = "site/" + page.name + "/" + pageImageFileRef.name;
      }
    }
    
    
    private function signupPageImageFileRefComplete(event:Event):void {
      signupImageForm.progressMessage.text += " (complete)";
      signupImageForm.progressBar.visible = false;
      signupImageForm.thumbnail.source = "site/sites/" + model.currentSiteId + "/images/" + signupPageImageFileRef.name;
      signupImageForm.loaded = true;
    }
    
    
    
    private function fileRefProgress(event:ProgressEvent):void {
      model.progressBarVisible = true;
    }
    
        
    private function signupPlaylistImageFileRefProgress(event:ProgressEvent):void {
      model.progressBarVisible = true;
    }



    private function fileRefComplete(event:Event):void {
      model.progressMessage += " (complete)";
      model.progressBarVisible = false;
      CairngormEventDispatcher.getInstance().dispatchEvent(new SiteEvent(SiteEvent.GET_SITE, model.currentSiteId));
    }

  } 
}
    